package com.test.pay.controller;

import com.test.pay.pojo.Order;
import com.test.pay.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("create.html")//创建订单 amount单位是元
    public String create(Float amount, Model model) {
        if (amount == null || amount < 0) {//金额不对
            model.addAttribute("message", "金额不对");
            return "pay.jsp";
        }
        Integer userId = 1;//模拟当前登录的用户
        String orderNum = System.currentTimeMillis() + "";
        amount = amount * 100;//转化成分
        Order order = new Order(userId, orderNum, amount.intValue(), "测试订单");
        orderService.addOrder(order);
        model.addAttribute("order", order);
        return "order.jsp";
    }

}
